package com.yjhb.android.sbz.popup;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.entity.Label;
import com.yjhb.android.sbz.manager.ResManager;
import com.qmuiteam.qmui.widget.QMUIFloatLayout;

import java.util.List;

/**
 * 创建者 zx
 * 创建时间 2020/12/15 9:51
 */
public class TypePopup extends PopupWindow {
    private final String TAG = "TypePopup";
    private final List<Label> labels;
    private Context context;
    private TextView currTextView;
    private Label currLabel;
    private onSelectedLabelListener onSelectedLabelListener;
    private QMUIFloatLayout floatLayout;

    public TypePopup(Context context, List<Label> labels) {
        super(context);
        this.context = context;
        this.labels = labels;
        init();
    }


    private void init() {
        View view = LayoutInflater.from(context).inflate(R.layout.popup_label, null, false);
        floatLayout = view.findViewById(R.id.floatLayout);
        setContentView(view);
        setWidth(-1);
        setHeight(-2);
        setBackgroundDrawable(new ColorDrawable(0));
        setOutsideTouchable(true);
        setAnimationStyle(R.style.popup_anim_alpha);
        refresh();
    }

    private void selected(TextView v, Label label) {
        currLabel = label;
        if (currTextView != null) {
            currTextView.setTextColor(Color.BLACK);
            currTextView.setBackground(new ColorDrawable(0));
        }
        currTextView = v;
        currTextView.setTextColor(Color.WHITE);
        currTextView.setBackground(new ColorDrawable(ResManager.getThemeColor()));
        if (onSelectedLabelListener != null) {
            onSelectedLabelListener.onSelectedLabel(label);
        }
    }

    public void selectedFirst() {
        if (floatLayout != null && labels != null && labels.size() > 1 && currLabel != labels.get(0))
            selected((TextView) floatLayout.getChildAt(0), labels.get(0));
    }

    public void refresh() {
        floatLayout.removeAllViews();
        for (final Label label : labels) {
            TextView textView = new TextView(context);
            textView.setText(label.getTitle());
            textView.setTag(label.getTag());
            textView.setGravity(Gravity.CENTER);
            textView.setTextSize(12);
            textView.setPadding(0, ResManager.dp() * 5, 0, ResManager.dp() * 5);
            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    selected((TextView) v, label);
                    if (onSelectedLabelListener != null) {
                        onSelectedLabelListener.onClickSelectedLabel(label);
                    }
                }
            });
            if (currLabel == label) {
                currTextView = textView;
                textView.setTextColor(Color.WHITE);
                textView.setBackground(new ColorDrawable(ResManager.getThemeColor()));
            } else {
                textView.setTextColor(Color.BLACK);
                textView.setBackground(new ColorDrawable(0));
            }
            floatLayout.addView(textView, ResManager.dp() * 65, -2);
        }
        //默认选中第一项
        selected((TextView) floatLayout.getChildAt(0), labels.get(0));
    }

    public void setOnSelectedLabelListener(TypePopup.onSelectedLabelListener onSelectedLabelListener) {
        this.onSelectedLabelListener = onSelectedLabelListener;
    }

    public interface onSelectedLabelListener {

        void onSelectedLabel(Label label);

        void onClickSelectedLabel(Label label);
    }

}
